188. Best Time to Buy and Sell Stock IV.cpp#268
Open
kaif969 wants to merge 2 commits intoSjxSubham:mainfrom
Open
188. Best Time to Buy and Sell Stock IV.cpp#268kaif969 wants to merge 2 commits intoSjxSubham:mainfrom
kaif969 wants to merge 2 commits intoSjxSubham:mainfrom
Conversation
Reviewer's GuideThis PR adds a new C++ solution for LeetCode 188 using a 3D memoized recursion that tracks the current day, buy/sell state, and remaining transactions to compute maximum profit. Class diagram for the new Solution class (Best Time to Buy and Sell Stock IV)classDiagram
class Solution {
+vector<vector<vector<int>>> dp
+int profit(vector<int>& prices, int i, int isBuy, int k)
+int maxProfit(int k, vector<int>& prices)
}
Solution : profit(prices, i, isBuy, k) uses dp
Solution : maxProfit(k, prices) uses profit(prices, i, isBuy, k)
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
Author
|
@SjxSubham Please 🙏 check this PR also. |
Contributor
Author
|
@SjxSubham, please merge this PR. |
Owner
|
@kaif969 U r only allowed to contribute upto two solutions only... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Title Format: 188. Best Time to Buy and Sell Stock IV.cpp
Intuition
We want to maximize profit by making at most
kstock transactions. Each day, we can choose to buy, sell, or skip. Using recursion with memoization (3D DP), we track the day, buy/sell state, and remaining transactions.Approach
Use a recursive function profit(prices, i, isBuy, k):
Base case: if we reach the end of prices or no transactions left, return 0.
If isBuy == 1: choose to buy today or skip.
If isBuy == 0: choose to sell today or skip.
Store results in a 3D DP array dp[i][isBuy][k] to avoid recomputation.
Finally, call profit(prices, 0, 1, k) starting from day 0 with buy allowed.
Code Solution (C++)
Related Issues
#261
By submitting this PR, I confirm that:
Summary by Sourcery
Summary by Sourcery
New Features: